//
//  CZTextView.swift
//  微博
//
//  Created by 霸气的刘吉庆 on 16/4/25.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit

class CZTextView: UITextView {

    //CZTextView 进行一些列的处理
    
    //由于textView没有placeHolder ,占位符
    //定义一个label,专门放textViewLabel
    
    //提供给外界设置占位字符
    var placeHolderText: String? {
        didSet{
            
            placeHolderLabel.text = placeHolderText
            
        }
        
    }
    //外界设置了当前textView的字体的话,需要将占位文字的字体一并设置
    
    override var font: UIFont? {
        
        didSet{
            placeHolderLabel.font = self.font
        }
        
        
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        //添加控件
        addSubview(placeHolderLabel)
        //添加约束
        placeHolderLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(5)
            make.top.equalTo(8)
            make.width.equalTo(self).offset(-10)
            
        }
        
        //添加代理用来监听textView的变化
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChange", name: UITextViewTextDidChangeNotification, object: nil)
        
        
        
    }
    
    //textView变化的时候,调用此方法,如果里面有内容影藏label
    
  @objc private  func textDidChange(){
        
    placeHolderLabel.hidden = hasText()
    
    }
    
    
    
    
    
    
    //懒加载label
    private lazy var placeHolderLabel: UILabel = {
        let label = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 12)
        
        label.text = "听说熊本去玩耍了~~~~"
        label.numberOfLines = 0
        
        return label
        
        }()
    
    deinit{
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
        
    }
    
    

}


